//
//  UIBezierController.m
//  UILbrary
//
//  Created by mac on 16/7/20.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "UIBezierController.h"
#import "Constant.h"
@implementation UIBezierController
- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, kAPP_With, 64)];
    label.text = @"用UIBezierPath 和 CAShapeLayer 画表盘曲线 \n关于UIBezierPath 的教程http://www.cnblogs.com/Hakim/p/5585198.html  这篇demo的来源https://github.com/qianlonglv/GaugeDemo";
    label.font = [UIFont systemFontOfSize:13.0];
    label.numberOfLines = 0;
    [self.view addSubview:label];
    CGPoint center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    //贝斯曲线 的路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:M_PI* (-195)/180  endAngle:M_PI * (15)/180 clockwise:YES];
    
    //用来画曲线
    CAShapeLayer * layer = [CAShapeLayer layer];
    //填充颜色  当设为 橘黄色的时候 如下图  设为透明为下图
    layer.fillColor = [UIColor clearColor].CGColor;
    //圆弧线条颜色
    layer.strokeColor = [UIColor colorWithRed:0.12 green:0.52 blue:0.23 alpha:1].CGColor;
    //圆弧线条两端的模式，kCALineCapSquare 直角   kCALineCapRound 圆角
    layer.lineCap = kCALineCapRound;
    //圆弧线段的宽度
    layer.lineWidth = 5;
    //给layer 添加路径
    layer.path = [path CGPath];
    
    [self.view.layer addSublayer:layer];
    
    
    for (int i = 0; i<51; i++) {
        
//        CGFloat totleAngle = 195 + 15;
//        CGFloat perAngle = totleAngle/50.f;
//        CGFloat startAngle = M_PI * (-195 + perAngle * i)/180.0;
//        CGFloat endAngle = startAngle + M_PI * perAngle/5.0/180.0 ;
        
        CGFloat perAngle = 210.f/50.f;
        CGFloat startAngle = M_PI * (-195 + i * perAngle)/180;
        CGFloat textAngle = M_PI * (195 - i * perAngle)/180;
        CGFloat endAngle = startAngle + M_PI * (perAngle/5)/180;
        
        UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:150 startAngle:startAngle endAngle:endAngle clockwise:YES];
        CAShapeLayer *layer1 = [CAShapeLayer layer];
        layer1.fillColor = [UIColor clearColor].CGColor;
        layer1.strokeColor = [UIColor redColor].CGColor;
        layer1.lineCap = kCALineCapButt;//如果设为 kCALineCapSquare 则没有间距 kCALineCapRound 也会连在一起，不过会有凹凸的连续
        layer1.lineWidth = 5.0;
        if (i%5 == 0) {
            layer1.strokeColor = [UIColor orangeColor].CGColor;
            layer1.lineWidth = 10.0;
            
            CGPoint point = [self calcCircleCoordinateWithCenter:center andWithAngle:textAngle andWithRadius:125];
            NSString *valueString = [NSString stringWithFormat:@"%d",i * 2];
            
            UIFont* font = [UIFont fontWithName:@"Helvetica-Bold" size:7];
            UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(point.x - 5, point.y - 5, 12, 12)];
            text.text = valueString;
            text.font = font;
            text.textColor = [UIColor colorWithRed:0.54 green:0.78 blue:0.91 alpha:1.0];
            text.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:text];
        }
        layer1.path = [path1 CGPath];
        [self.view.layer addSublayer:layer1];
    }
    
}

- (CGPoint) calcCircleCoordinateWithCenter:(CGPoint) center  andWithAngle : (CGFloat) angle andWithRadius: (CGFloat) radius{
    CGFloat x2 = radius*cosf(angle);
    CGFloat y2 = radius*sinf(angle);
    return CGPointMake(center.x+x2, center.y-y2);
}

@end
